/*******************************************************************************
 * Copyright 2011 WaTho
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package de.wathoserver.android.dragoid.activity.tabs;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import de.wathoserver.android.dragoid.R;
import de.wathoserver.android.dragoid.activity.singlebuilding.SingleBuildingActivity;
import de.wathoserver.android.dragoid.api.Building;
import de.wathoserver.android.dragoid.api.DragoApi;
import de.wathoserver.android.dragoid.util.DialogHelper;
import de.wathoserver.android.dragoid.util.UrlParsingActivity;
import de.wathoserver.android.dragoid.util.UrlParsingTask;

/**
 * Zeigt eine Liste mit dem Zustand aller Gebäude an. (Reiter Z)
 * 
 * @author watho
 * 
 */
public class ConditionActivity extends ListActivity implements
		UrlParsingActivity<List<Map<String, String>>> {
	private String[] col_names;
	private int[] view_ids;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		col_names = new String[] { Building.COL_NAME, Building.COL_CONDITION };
		view_ids = new int[] { R.id.building_name, R.id.building_condition };
		reload();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void reload() {
		new UrlParsingTask<List<Map<String, String>>>(this, this).execute();
	}

	@Override
	public List<Map<String, String>> fetchData() throws IllegalAccessException {
		return DragoApi.getApi().getBuildingsWithCondition();
	}

	@Override
	public void onSuccess(List<Map<String, String>> result) {
		setListAdapter(new SimpleAdapter(this, result, R.layout.conditionview,
				col_names, view_ids));
		ListView lv = getListView();
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				@SuppressWarnings("unchecked")
				Map<String, String> building = (Map<String, String>) parent
						.getItemAtPosition(position);
				Intent intent = new Intent(ConditionActivity.this,
						SingleBuildingActivity.class);
				intent.putExtra(Building.COL_POSITION,
						Integer.parseInt(building.get(Building.COL_POSITION)));
				startActivity(intent);
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return DialogHelper.createDialogById(this, DIALOG_PROGRESS);
	}

	@Override
	public Activity getUserInfoActivity() {
		return this.getParent();
	}
}
